package com.lastproject.web.bean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.lastproject.dto.QuestionDTO;
import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.ejb.dao.QuestionDAORemote;
import com.lastproject.ejb.dao.QuestionOptionDAORemote;

@ManagedBean
@SessionScoped
public class QuestionBean {
	
	@EJB
	QuestionDAORemote questionDAO;
	@EJB
	QuestionOptionDAORemote questionOptionDAO;

	private QuestionDTO questionDTO; 
	private List<QuestionOptionBean> list;
	private List<QuestionOptionBean> simple;
	private List<QuestionOptionBean> multiple;
	private List<QuestionOptionBean> text;
	private List<QuestionOptionBean> rank;
	private List<Integer> selectedItems;
	private Integer selectedItem;
	
	private QuestionOptionBean qob;
	public QuestionOptionBean getQob() {
		return qob;
	}
	public void setQob(QuestionOptionBean qob) {
		this.qob = qob;
	}

	public List<QuestionOptionBean> getSimple() {
		return simple;
	}

	public void setSimple(List<QuestionOptionBean> simple) {
		this.simple = simple;
	}

	public List<QuestionOptionBean> getMultiple() {
		return multiple;
	}

	public void setMultiple(List<QuestionOptionBean> multiple) {
		this.multiple = multiple;
	}

	public List<QuestionOptionBean> getText() {
		return text;
	}

	public void setText(List<QuestionOptionBean> text) {
		this.text = text;
	}

	public List<QuestionOptionBean> getRank() {
		return rank;
	}

	public void setRank(List<QuestionOptionBean> rank) {
		this.rank = rank;
	}

	public QuestionDTO getQuestionDTO() {
		return questionDTO;
	}

	public void setQuestionDTO(QuestionDTO questionDTO) {
		this.questionDTO = questionDTO;
	}

	public List<QuestionOptionBean> getList() {
		return list;
	}

	public void setList(List<QuestionOptionBean> list) {
		this.list = list;
	}
	
	public Integer getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(Integer selectedItem) {
		this.selectedItem = selectedItem;
	}

	public List<Integer> getSelectedItems() {
		return selectedItems;
	}
	public void setSelectedItems(List<Integer> selectedItems) {
		this.selectedItems = selectedItems;
	}
	
	/**
	 * retrieves all question options (a question option represents a QuestionOptionBean) for of the logged in user
	 * @return a list of SurveyBeans
	 */
	public List<QuestionOptionBean> getAllQuestionOptionBean(int id) {
		List<QuestionOptionBean> list = new ArrayList<>();
		List<QuestionOptionDTO> aux = new ArrayList<>();
	
		aux.addAll(questionOptionDAO.getQuestionOptionListByQuestionId(id));

		for (QuestionOptionDTO option : aux) {
			QuestionOptionBean q = new QuestionOptionBean();
			q.setQuestionOptionDTO(option);
			list.add(q);
		}
		return list;
	}
	
	/**
	 * verifies which questions have been answered and saves them by calling the addAnswer method
	 * in the QuestionOptionBean. If no question is answered a boolean value stores this and an error 
	 * message will be displayed
	 * @return a boolean value true if any question is answered and false otherwise
	 */
	public boolean save(QuestionBean qb) {
		//for single answers
		boolean ok=false;
		if(qb.getSelectedItem()!=null){
		for(QuestionOptionBean q:qb.getList())
			if(q.getQuestionOptionDTO().getId()==qb.getSelectedItem()){
				qob.addAnswer(q.getQuestionOptionDTO().getText(), q.getQuestionOptionDTO().getId());
				ok=true;
			}}
		else if(!qb.getSimple().isEmpty())
			return false;
		//for multiple answers
		if(!qb.getSelectedItems().isEmpty()){
		for(Integer i:qb.getSelectedItems())
			for(QuestionOptionBean q:qb.getList())
				if(q.getQuestionOptionDTO().getId()==i){
					qob.addAnswer(q.getQuestionOptionDTO().getText(), q.getQuestionOptionDTO().getId());
					ok=true;
				}}
		else if(!qb.getMultiple().isEmpty())
			return false;
		//for text answers
		if(!qb.getText().isEmpty())
			for(QuestionOptionBean q:qb.getText())
				if(!q.getQuestionOptionDTO().getTextDefault().trim().
						equals(questionOptionDAO.findById(q.getQuestionOptionDTO().getId()).getTextDefault().trim())){
					qob.addAnswer(q.getQuestionOptionDTO().getTextDefault(),q.getQuestionOptionDTO().getId());
					ok=true;
				}
		//for grades
		if(!qb.getRank().isEmpty()){
			boolean allFilled=true;
			for(QuestionOptionBean q:qb.getRank())
				if(q.getQuestionOptionDTO().getTextDefault().isEmpty())
					allFilled=false;
			if(allFilled)
				for(QuestionOptionBean q:qb.getRank()){
					qob.addAnswer(q.getQuestionOptionDTO().getTextDefault(),q.getQuestionOptionDTO().getId());
					ok=true;
				}
		}
		return ok;
	 }	
	
	private String rankError;
	public String getRankError() {
		return rankError;
	}
	public void setRankError(String rankError) {
		this.rankError = rankError;
	}
}
